<template>
	<view class="default-background">
		<view class="home-page">
			<view class="title-bar" @click="toEdit">
				<view class="user-info-group">
					<view class="info-name">{{userInfo.username}}</view>
					<view class="info-phone" @click="copyPhone(userInfo.phone)">{{userInfo.phone}}</view>
				</view>
				<image :src="userInfo.logo ? userInfo.logo : '../../static/logo.png'" class="user-logo" mode="aspectFill"></image>
			</view>
			<scroll-view>
				<view class="house-item" v-for="house in this.houseList" @click="getDetail(house.id)">
					<view class="img-group">
						<image :src="house.firstImg" class="first-img" mode="aspectFill"></image>
					</view>
					<view class="text-group">
						<view class="title">{{house.title}}</view>
						<view class="price" v-if="house.type==0">{{house.price}}元/每月</view>
						<view class="price" v-if="house.type==1">{{house.price}}元</view>
						<view class="introduce limit-one-line">{{house.introduce}}</view>
						<!-- <uni-dateformat :date="house.createTime" format="yyyy-MM-dd hh:mm:ss" class="create-time"></uni-dateformat> -->
					</view>
				</view>
				<view class="page-bottom"></view>
			</scroll-view>
			<view class="btn-group">
				<view class="confirmBtn button" @click="toChat()">咨询</view>
			</view>
		</view>
	</view>
</template>

<script>
	import config  from "@/config/app.js"
	export default {
		data() {
			return {
				userId: "",
				page: 1,
				limit: 6,
				userInfo: {},
				houseList: []
			};
		},
		methods: {
			getUserInfo() {
				this.$request.request({
					url: "/user/getHomePageInfo",
					method: "GET",
					data: {
						userId: this.userId,
						page: this.page,
						limit: this.limit
					}
				}).then(res=>{
					if (res.code == 200) {
						this.userInfo = res.data.userInfo;
						this.userInfo.logo = config.IMG_URL + this.userInfo.logo;
						if (res.data.houseList.length <= 0) {
							uni.showToast({
								title: "没有更多数据了~",
								icon: "none"
							})
							return;
						} else {
						// 为图片添加图片地址
							for (let i=0; i<res.data.houseList.length; i++) {
								res.data.houseList[i].firstImg = config.IMG_URL + JSON.parse(res.data.houseList[i].pic)[0]
							}
							this.houseList = this.houseList.concat(res.data.houseList);
						}
					} else if (res.code == 401) {
						uni.navigateTo({
							url: "../user/login"
						})
					}
				})
			},
			getDetail(id) {
				uni.redirectTo({
					url: "../house/detail?id=" + id
				})
			},
			copyPhone(phone) {
				uni.setClipboardData({
				  data:value,//要被复制的内容
				  success:()=>{//复制成功的回调函数
						uni.showToast({//提示
							title:'手机号复制成功'
						})
				  }
				});
			},
			toChat() {
				uni.navigateTo({
					url: "../chat/chatPage?receiverId=" + this.userId
				})
			}
		},
		onLoad(option) {
			this.userId = option.userId;
			this.getUserInfo();
		},
		onShow() {
		},
		onPullDownRefresh() {
			this.page = 1;
			this.houseList = [];
			this.searchText = "";
			this.getUserInfo();
			uni.stopPullDownRefresh();
		},
		async onReachBottom() {
			this.page += 1;
			this.getUserInfo();
		}
	}
</script>

<style lang="scss">
 .home-page {
	 
	 .title-bar {
	 	padding: 20rpx 50rpx;
	 	background-color: #11A8CD;
	 	color: #FFFFFF;
	 	display: flex;
	 	
	 	.user-logo {
	 		width: 120rpx;
	 		height: 120rpx;
	 		margin: 20rpx 20rpx;
	 		background-color: #FFFFFF;
	 		border-radius: 120rpx;
	 	}
	 	
	 	.user-info-group {
	 		flex: 1;
	 		color: #FFFFFF;
	 		padding: 25rpx 20rpx;
	 		
	 		.info-name {
	 			font-size: 18px;
	 			
	 		}
	 		
	 		.info-phone {
	 			margin-top: 10rpx;
	 			font-size: 16px;
	 		}
	 	}
		
	 }
	 
	 .house-item {
	 	background-color: #FFFFFF;
	 	padding: 20rpx;
	 	display: flex;
	 	border-bottom: 3rpx solid #F2F1F6;
	 	
	 	.img-group {
	 		
	 		.first-img {
	 			width: 300rpx;
	 			height: 180rpx;
	 			border-radius: 20rpx;
	 		}
	 	}
	 	
	 	.text-group {
	 		padding: 0 20rpx;
	 		flex: 1;
	 		width: 360rpx;
	 		
	 		.title {
	 			font-size: 20px;
	 		}
	 		
	 		.price {
	 			margin-top: 30rpx;
	 			color: #F6416C;
	 		}
	 		
	 		.introduce {
	 			height: 40rpx;
	 			font-size: 16px;
	 			color: #999999;
	 		}
	 		
	 		.create-time {
	 			font-size: 16px;
	 			color: #999999;
	 		}
	 		
	 	}
	 }
 }
</style>
